/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import commons.ExceptionCLLAMB;
import java.sql.Statement;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author xmarti
 */
public class ManagerConnection {
    Connection connection;
    Statement statement=null;
    public void openConnection() throws ExceptionCLLAMB {    
     try{
        Class.forName("org.postgresql.Driver");
        Properties props = new Properties();
        URL url = ClassLoader.getSystemResource("properties/configuration.properties");
        props.load(url.openStream());      
        connection = DriverManager.getConnection(props.getProperty("url"),
                props.getProperty("username"),props.getProperty("password"));
        } catch (SQLException sqle){    
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERRCONNECTDB);    
        }catch(IOException ie){
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERRNOFILE);
        }catch (ClassNotFoundException cnfe){
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERRNOCLASS);
        }catch (Exception e){    
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERR);
        }
    }
    public Statement createStatement() throws ExceptionCLLAMB{
        
        try {
           statement=connection.createStatement();                          
        } catch (SQLException ex) {
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERRCONNECTDB);    
        }
       return statement; 
    }
    public PreparedStatement createPreparedStatement(String ps) throws ExceptionCLLAMB{        
        try{
           return connection.prepareStatement(ps, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        }catch(SQLException sqle){
            throw new ExceptionCLLAMB(ExceptionCLLAMB.ERRCONNECTDB);    
        }
    }
    public Connection getConnection (){
        return connection;         
    }
    public void setConnection(Connection pConnection){
        this.connection=pConnection;
    }
}
